import 'package:bluetooth_protocols_parser_plugin/library.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

import 'firmware_info_model.dart';
import 'loading.dart';
import 'utils.dart';

class LorinPage extends StatefulWidget {
  const LorinPage({super.key});

  @override
  State<LorinPage> createState() => _LorinPageState();
}

class _LorinPageState extends State<LorinPage> {
  // 协议类型
  late Protocol _protocol;

  RxString sn = ''.obs;
  RxString productModel = ''.obs;
  RxList<FirmwareInfoModel> firmwareInfoModels = <FirmwareInfoModel>[].obs;

  // 低速档速度限制
  RxInt lSpeedLimit = 0.obs;

  // 中速档速度限制
  RxInt mSpeedLimit = 0.obs;

  // 高速档速度限制
  RxInt hSpeedLimit = 0.obs;

  // 样式
  final TextStyle style = const TextStyle(
    fontSize: 16,
    fontWeight: FontWeight.w500,
    color: Colors.black,
  );

  // rs-美规
  List<int> rsSpeedLimitWide = <int>[32, 64, 100, 130];

  // rs-其他
  List<int> rsSpeedLimitOther = <int>[25, 65, 90, 100];

  @override
  void initState() {
    super.initState();
    // 只限于滑板车，由于用到的是公共组指令，故这里直接使用 RsJetProtocol
    _protocol = RsJetProtocol();
    _getBaseInfo();
  }

  _getBaseInfo() {
    Future.delayed(const Duration(milliseconds: 500)).then((value) {
      // sn
      return _protocol.getSn();
    }).then((snRsp) {
      sn.value = snRsp.data2Text;
      // 产品模型
      return _protocol.getProductModel();
    }).then((productModelRsp) {
      productModel.value = Utils.uint8List2HexString(productModelRsp.data);
      // 固件信息
      return _protocol.getFirmwareVersion();
    }).then((firmwareInfoRsp) {
      firmwareInfoModels.value = firmwareInfoRsp
          .map((item) => FirmwareInfoModel.fromMap(item))
          .toList();
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        actions: [
          ElevatedButton(
            onPressed: () async {
              try {
                Loading.show(null);
              } catch (e) {
                Get.snackbar('Error', e.toString());
              } finally {
                Loading.dismiss();
              }
            },
            child: const Text('Restore speed limit'),
          ),
        ],
      ),
      body: Container(
        padding: const EdgeInsets.all(16),
        color: Colors.white,
        width: double.infinity, // Make container fill width
        height: double.infinity, // Make container fill height
        child: SingleChildScrollView(
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Obx(
                () => Text(
                  'SN: ${sn.value}',
                  style: style,
                ),
              ),
              Obx(
                () => Text(
                  'Product Model: ${productModel.value}',
                  style: style,
                ),
              ),
              // 显示固件信息
              const SizedBox(height: 20),
              Obx(
                () => Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: firmwareInfoModels.map((model) {
                    return Text(
                      '${model.deviceTypeName}-${model.deviceId}: v${model.bootVersion}',
                      style: style,
                    );
                  }).toList(),
                ),
              ),
              const SizedBox(height: 20),
              Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  SizedBox(
                    width: context.width - 40,
                    height: 40,
                    child: ElevatedButton(
                      onPressed: () async {
                        try {
                          Loading.show(null);
                        } catch (e) {
                          Get.snackbar('Error', e.toString());
                        } finally {
                          Loading.dismiss();
                        }
                      },
                      child: const Text('Apply'),
                    ),
                  ),
                ],
              ),
            ],
          ),
        ),
      ),
    );
  }
}
